package com.tanhua.recommend.listener;

import com.alibaba.fastjson.JSON;
import com.tanhua.domain.mongo.Publish;
import com.tanhua.domain.mongo.PublishScore;
import com.tanhua.dubbo.api.mongo.PublishApi;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-quanzi", consumerGroup = "tanhua-quanzi-group")
public class PublishScoreListener implements RocketMQListener<String> {

    @Reference
    private PublishApi publishApi;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void onMessage(String message) {
        //1.把消息转换为Map
        Map<String,Object> map = JSON.parseObject(message, Map.class);
        //2. 从map中取出数据
        Long userId = Long.valueOf(map.get("userId").toString());
        String publishId = map.get("publishId").toString();
        Long pid = Long.valueOf(map.get("pid").toString());
        Integer type = Integer.valueOf(map.get("type").toString());

        //3. 创建publishScore对象， 补全数据
        PublishScore ps = new PublishScore();
        ps.setUserId(userId);
        ps.setPublishId(pid);
        ps.setDate(System.currentTimeMillis());

        //4. 判断用户的行为计算当前操作的得分
        // // 1-发动态，2-浏览动态，3-点赞，4-喜欢，5-评论，6-取消点赞，7-取消喜欢
        switch (type) {
            case 1:
                Integer score = 0;
                //找到当前发布的动态
                Publish publish = publishApi.findById(publishId);
                String textContent = publish.getTextContent();
                //算出文字的得分
                if (StringUtils.length(textContent) < 50) {
                    score += 1;
                } else if (StringUtils.length(textContent) <= 100) {
                    score += 2;
                } else if (StringUtils.length(textContent) > 100) {
                    score += 3;
                }

                //计算图片的得分
                score += publish.getMedias().size();

                ps.setScore(score.doubleValue());
                break;
            case 2: {
                ps.setScore(1d);
                break;
            }
            case 3: {
                ps.setScore(5d);
                break;
            }
            case 4: {
                ps.setScore(8d);
                break;
            }
            case 5: {
                ps.setScore(10d);
                break;
            }
            case 6: {
                ps.setScore(-5d);
                break;
            }
            case 7: {
                ps.setScore(-8d);
                break;
            }
            default: {
                ps.setScore(0d);
                break;
            }
        }

        //5. 插入数据
        mongoTemplate.save(ps);
    }
}
